<template>
  <view class="bag-container">
    <!-- 顶部标签页 -->
    <view class="tab-bar">
      <view 
        class="tab-item"
        v-for="tab in tabs"
        :key="tab.id"
        :class="{ 'active': currentTab === tab.id }"
        @click="switchTab(tab.id)"
      >
        <text class="tab-text">{{tab.name}}</text>
      </view>
    </view>

    <!-- 物品列表 -->
    <scroll-view class="item-list" scroll-y>
      <view class="item-grid">
        <view 
          class="item-card"
          v-for="item in filteredItems"
          :key="item.id"
          @click="showItemDetail(item)"
        >
          <image class="item-icon" :src="item.icon" mode="aspectFit"></image>
          <view class="item-info">
            <text class="item-name" :class="{'rare': item.rare}">{{item.name}}</text>
            <text class="item-count">×{{item.count}}</text>
          </view>
        </view>
      </view>
    </scroll-view>

    <!-- 物品详情弹窗 -->
    <uni-popup ref="itemDetailPopup" type="center">
      <view class="item-detail">
        <view class="detail-header">
          <text class="detail-title">物品详情</text>
          <text class="close-btn" @click="closeItemDetail">×</text>
        </view>
        <view class="detail-content" v-if="selectedItem">
          <image class="detail-icon" :src="selectedItem.icon" mode="aspectFit"></image>
          <text class="detail-name" :class="{'rare': selectedItem.rare}">{{selectedItem.name}}</text>
          <text class="detail-desc">{{selectedItem.description}}</text>
          <view class="detail-attrs">
            <view class="attr-item" v-for="(value, key) in selectedItem.attributes" :key="key">
              <text class="attr-label">{{getAttrLabel(key)}}</text>
              <text class="attr-value">{{value}}</text>
            </view>
          </view>
          <view class="detail-actions">
            <button 
              class="action-btn use-btn" 
              v-if="selectedItem.usable"
              @click="useItem(selectedItem)"
            >使用</button>
            <button 
              class="action-btn sell-btn"
              @click="sellItem(selectedItem)"
            >出售</button>
          </view>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
export default {
  data() {
    return {
      tabs: [
        { id: 'all', name: '全部' },
        { id: 'equipment', name: '装备' },
        { id: 'consumable', name: '消耗品' },
        { id: 'material', name: '材料' }
      ],
      currentTab: 'all',
      items: [
        {
          id: 1,
          name: '木剑',
          type: 'equipment',
          icon: '/static/items/wooden_sword.png',
          count: 1,
          rare: false,
          description: '新手常用的木制剑',
          attributes: {
            attack: 5,
            durability: 100
          },
          usable: true
        },
        {
          id: 2,
          name: '小型药水',
          type: 'consumable',
          icon: '/static/items/potion.png',
          count: 5,
          rare: false,
          description: '恢复少量生命值',
          attributes: {
            heal: 20
          },
          usable: true
        },
        {
          id: 3,
          name: '铁矿石',
          type: 'material',
          icon: '/static/items/iron_ore.png',
          count: 10,
          rare: false,
          description: '用于锻造装备的原材料',
          attributes: {
            quality: '普通'
          },
          usable: false
        }
      ],
      selectedItem: null
    }
  },
  computed: {
    filteredItems() {
      if (this.currentTab === 'all') {
        return this.items
      }
      return this.items.filter(item => item.type === this.currentTab)
    }
  },
  methods: {
    switchTab(tabId) {
      this.currentTab = tabId
    },
    showItemDetail(item) {
      this.selectedItem = item
      this.$refs.itemDetailPopup.open()
    },
    closeItemDetail() {
      this.$refs.itemDetailPopup.close()
      this.selectedItem = null
    },
    getAttrLabel(key) {
      const labels = {
        attack: '攻击力',
        defense: '防御力',
        durability: '耐久度',
        heal: '恢复量',
        quality: '品质'
      }
      return labels[key] || key
    },
    useItem(item) {
      if (!item.usable) {
        uni.showToast({
          title: '该物品不可使用',
          icon: 'none'
        })
        return
      }

      // 根据物品类型执行不同的使用逻辑
      switch (item.type) {
        case 'equipment':
          // 装备逻辑
          uni.showToast({
            title: '装备成功',
            icon: 'success'
          })
          break
        case 'consumable':
          // 消耗品逻辑
          item.count--
          if (item.count <= 0) {
            this.items = this.items.filter(i => i.id !== item.id)
          }
          uni.showToast({
            title: '使用成功',
            icon: 'success'
          })
          break
      }

      this.closeItemDetail()
    },
    sellItem(item) {
      // 计算售价
      const price = Math.floor(Math.random() * 50 + 10)
      
      uni.showModal({
        title: '出售确认',
        content: `确定要以${price}金币的价格出售该物品吗？`,
        success: (res) => {
          if (res.confirm) {
            item.count--
            if (item.count <= 0) {
              this.items = this.items.filter(i => i.id !== item.id)
            }
            // TODO: 更新玩家金币
            uni.showToast({
              title: '出售成功',
              icon: 'success'
            })
            this.closeItemDetail()
          }
        }
      })
    }
  }
}
</script>

<style>
.bag-container {
  min-height: 100vh;
  background: #f5f5f5;
  display: flex;
  flex-direction: column;
}

.tab-bar {
  background: white;
  display: flex;
  padding: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.1);
}

.tab-item {
  flex: 1;
  text-align: center;
  padding: 20rpx 0;
  position: relative;
}

.tab-item.active {
  color: #4caf50;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 40rpx;
  height: 4rpx;
  background: #4caf50;
  border-radius: 2rpx;
}

.tab-text {
  font-size: 28rpx;
}

.item-list {
  flex: 1;
  padding: 20rpx;
}

.item-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20rpx;
}

.item-card {
  background: white;
  border-radius: 16rpx;
  padding: 20rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.item-icon {
  width: 80rpx;
  height: 80rpx;
  margin-bottom: 10rpx;
}

.item-info {
  width: 100%;
  text-align: center;
}

.item-name {
  font-size: 24rpx;
  color: #333;
  margin-bottom: 6rpx;
}

.item-name.rare {
  color: #f57c00;
}

.item-count {
  font-size: 24rpx;
  color: #666;
}

.item-detail {
  background: white;
  border-radius: 20rpx;
  width: 600rpx;
  padding: 30rpx;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30rpx;
}

.detail-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.close-btn {
  font-size: 40rpx;
  color: #999;
  padding: 0 20rpx;
}

.detail-content {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.detail-icon {
  width: 120rpx;
  height: 120rpx;
  margin-bottom: 20rpx;
}

.detail-name {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 10rpx;
}

.detail-name.rare {
  color: #f57c00;
}

.detail-desc {
  font-size: 28rpx;
  color: #666;
  margin-bottom: 30rpx;
  text-align: center;
}

.detail-attrs {
  width: 100%;
  margin-bottom: 30rpx;
}

.attr-item {
  display: flex;
  justify-content: space-between;
  padding: 10rpx 0;
  border-bottom: 1rpx solid #eee;
}

.attr-label {
  font-size: 28rpx;
  color: #666;
}

.attr-value {
  font-size: 28rpx;
  color: #333;
}

.detail-actions {
  display: flex;
  justify-content: space-around;
  width: 100%;
}

.action-btn {
  width: 200rpx;
  height: 80rpx;
  line-height: 80rpx;
  border-radius: 40rpx;
  font-size: 28rpx;
  margin: 0;
}

.use-btn {
  background: #4caf50;
  color: white;
}

.sell-btn {
  background: #ff9800;
  color: white;
}
</style> 